
package com.esp.app.wifi;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

import com.esp.app.widget.MyWidgetProvider;
import com.esp.app.widget.R;

public class ViewHandler {

    private static final String TAG = ViewHandler.class.getName();

    public static void updateAppWidgets(final Context context, final AppWidgetManager appWidgetManager,
                    final int[] appWidgetIds, final int resource) {

        Log.d(TAG, "Wifi ViewHandler |=>  updateAppWidgets ");

        for (int i = 0; i < appWidgetIds.length; i++) {
            final int appWidgetId = appWidgetIds[i];
            updateAppWidget(context, appWidgetManager, appWidgetId, resource);
        }

    }

    public static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId,
                    final int resource) {

    	Log.d(TAG, "Wifi ViewHandler |=>  updateAppWidget ");

        final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        
        final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(MyWidgetProvider.TOGGLE_WIFI_STATE),
                PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.btn_wifi, pendingIntent);
        
        views.setImageViewResource(R.id.btn_wifi,resource);
        
        appWidgetManager.updateAppWidget(appWidgetId, views);
        

    }

}
